<?php
	defined('INDEX') or die ('Acesso direto n&atilde;o permitido');
	
	class Application
	{
		private static $pageContent 			= null;
		
		/**
		 * Global instance for FormularioBase object.
		 * 
		 * @var FormularioBase
		 */
		private static $formularioBaseInstance 	= null;
		
		public static function setCurrentFormularioBase (FormularioBase $formularioBase)
		{
			if(self::$formularioBaseInstance == null)
			{
				self::$formularioBaseInstance = $formularioBase;
			}
		}
		
		/**
		 * Sets the page charset.
		 * 
		 * @param string $charset
		 * 
		 * @return void
		 */
		public static function setPageCharset ($charset = 'UTF-8')
		{
			if($charset == null || $charset == '')
			{
				$charset = 'UTF-8';
			}
			
			header("Content-type: text/html; charset=".$charset);
		}
		
		/**
		 * Start the local server buffer to process and yet starts 
		 * headers and force internal redirects.
		 * 
		 * @return void
		 */
		public static function ob_start ()
		{
			ob_start();
		}
		
		/**
		 * Close de local server page buffer and prints to send to
		 * application user address.
		 * 
		 * @return string $content
		 */
		public static function ob_end_clean ()
		{
			self::$pageContent = ob_get_contents();
			ob_end_clean();
		}
		
		/**
		 * This function process all init and render application process.
		 * 
		 * @return void
		 */
		public static function initApplication ()
		{
			Application::setPageCharset('UTF-8');
			Application::ob_start();
			
			require_once(dirname(__FILE__)."/ApplicationFactory.class.php");
			
			$request = ApplicationFactory::getRequestFactory();
			
			$pageRequested 	= null;
			$pageObject		= null;
			
			if($request->hasPost())
			{
				$pageRequested = $request->post('pagina', 'STRING');
			}
			else
			{
				$pageRequested = $request->get('pagina', 'STRING');
			}
			
			$pageAccessControl = ApplicationFactory::getPageAccessControlFactory();
			
			if($pageRequested)
			{
				if($pageAccessControl->hasPage($pageRequested))
				{
					$pageObject = $pageAccessControl->getPage($pageRequested);
				}
				else
				{
					$content = new stdClass();
					
		            $content->type        = 3;
		            $content->mensagem    = "A página requisitada não existe.";
		            $content->title       = "Página não encontrada";
		            
		            ApplicationFactory::getSessionFactory()->storeMessage($content);
				}
			}
			
			if($pageObject == null)
			{
				$pageObject = $pageAccessControl->getPage('home');
			}
			$pageObject->processAction();
			
			ApplicationFactory::getDebugFactory()->showDebugInformation();
			
			Application::ob_end_clean();
			Application::processContentHidratation();
		}
		
		public static function processContentHidratation ()
		{
			$content = self::$pageContent;
			
			$component = ApplicationFactory::getHTMLComponentFactory();
			
			$content = 
				$component->getHTMLHeader(self::$formularioBaseInstance->getPageTitle()).
				$component->getHTMLUserBar().
				$content.
				$component->getHTMLClosePage();
			
			
			$beforeScripts = '';
			
			foreach ($component->getRequestedBeforeLoadJavascriptFiles() as $script)
			{
				$beforeScripts .= $script;
			}
			
			$afterScripts = '';
			
			foreach ($component->getRequestedAfterLoadJavascriptFiles() as $script)
			{
				$afterScripts .= $script;
			}
			
			$cssFiles = '';
			foreach ($component->getRequestedCssFiles() as $css)
			{
				$cssFiles .= $css;
			}
			
			if($cssFiles)
			{
				$content = str_replace ('</head>', $cssFiles."</head>", $content);
			}
			
			if($beforeScripts)
			{
				$content = str_replace ('</head>', $beforeScripts."</head>", $content);
			}
			
			if($afterScripts)
			{
				$content = str_replace ('</body>', $afterScripts."</body>", $content);
			}
			
			echo $content;
		}
	}
?>